package zest;

import java.awt.List;
import java.util.LinkedList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphConnection;
import org.eclipse.zest.core.widgets.GraphNode;
import org.eclipse.zest.core.widgets.ZestStyles;
import org.eclipse.zest.layouts.LayoutStyles;
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;

public class VerGrafoZestExcecoes extends ViewPart {
  public static final String ID = "zest.VerGrafoZestExcecoes";
  private Graph graph;
  private int layout = 1;
  private Composite parent;

  public void createPartControl(final Composite parent) {
    // Graph will hold all other objects
    graph = new Graph(parent, SWT.NONE);
    this.parent = parent;
    // now a few nodes
    /*GraphNode node1 = new GraphNode(graph, SWT.NONE, "Jim");
    GraphNode node2 = new GraphNode(graph, SWT.NONE, "Jack");
    GraphNode node3 = new GraphNode(graph, SWT.NONE, "Joe");
    GraphNode node4 = new GraphNode(graph, SWT.NONE, "Bill");
    // Lets have a directed connection
    new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, node1,
        node2);
    // Lets have a dotted graph connection
    new GraphConnection(graph, ZestStyles.CONNECTIONS_DOT, node2, node3);
    // Standard connection
    new GraphConnection(graph, SWT.NONE, node3, node1);
    // Change line color and line width
    GraphConnection graphConnection = new GraphConnection(graph, SWT.NONE,
        node1, node4);
    graphConnection.changeLineColor(parent.getDisplay().getSystemColor(SWT.COLOR_GREEN));
    // Also set a text
    graphConnection.setText("This is a text");
    graphConnection.setHighlightColor(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
    graphConnection.setLineWidth(3);*/

    graph.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
    // Selection listener on graphConnect or GraphNode is not supported
    // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=236528
    graph.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
    	 if(e.item instanceof GraphConnection)
    	 {
    		 //usuario acaba de clicar numa aresta. vamos pegar o texto da aresta e colorir todas as arestas de mesmo texto
    		 GraphConnection arestaClicada = (GraphConnection)e.item;
    		 String textoArestaClicada = arestaClicada.getText();
    		 java.util.List<GraphConnection> listaArestas = graph.getConnections();
    		 for(int i = 0; i < listaArestas.size(); i++)
    		 {
    			 GraphConnection aresta = listaArestas.get(i);
    			 String textoAresta = aresta.getText();
    			 if(textoAresta.compareTo(textoArestaClicada) == 0)
    			 {
    				 
    				 aresta.changeLineColor(parent.getDisplay().getSystemColor(SWT.COLOR_GREEN));
    				 aresta.setHighlightColor(parent.getDisplay().getSystemColor(SWT.COLOR_GREEN));
    			 }
    			 else
    			 {
    				 aresta.changeLineColor(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
    				 aresta.setHighlightColor(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
    			 }
    		 }
    	 }
        System.out.println(e);
      }

    });
  }

  public void setLayoutManager() {
    switch (layout) {
    case 1:
      graph.setLayoutAlgorithm(new TreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
      layout++;
      break;
    case 2:
      graph.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
      layout = 1;
      break;

    }

  }

  
/**
   * Passing the focus request to the viewer's control.
   */

  public void setFocus() {
  }
  
  public Graph getGrafo()
  {
	  return graph;
  }
  
  public Graph getGrafoLimpo()
  {
	  
	  java.util.List nodes= graph.getNodes();
	  int s=nodes.size();
	  for (int i = s-1; i >= 0; i--) {
	  ((GraphNode ) nodes.get(i)).dispose();
	  }
	  
	  
	  graph.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
	    // Selection listener on graphConnect or GraphNode is not supported
	    // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=236528
	    graph.addSelectionListener(new SelectionAdapter() {
	      @Override
	      public void widgetSelected(SelectionEvent e) {
	    	 if(e.item instanceof GraphConnection)
	     	 {
	     		 //usuario acaba de clicar numa aresta. vamos pegar o texto da aresta e colorir todas as arestas de mesmo texto
	     		 GraphConnection arestaClicada = (GraphConnection)e.item;
	     		 String textoArestaClicada = arestaClicada.getText();
	     		 java.util.List<GraphConnection> listaArestas = graph.getConnections();
	     		 for(int i = 0; i < listaArestas.size(); i++)
	     		 {
	     			 GraphConnection aresta = listaArestas.get(i);
	     			 String textoAresta = aresta.getText();
	     			 if(textoAresta.compareTo(textoArestaClicada) == 0)
	     			 {
	     				 
	     				 aresta.changeLineColor(parent.getDisplay().getSystemColor(SWT.COLOR_GREEN));
	     				aresta.setHighlightColor(parent.getDisplay().getSystemColor(SWT.COLOR_GREEN));
	     			 }
	     			 else
	     			 {
	     				 aresta.changeLineColor(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
	     				aresta.setHighlightColor(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
	     			 }
	     		 }
	     	 }  
	        System.out.println(e);
	      }

	    });
	    return graph;
	  
  }
} 